package remato.client.model;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.swt.graphics.Image;

import remato.client.controller.RequirementPlugin;
import static remato.client.controller.PrecisePluginImage.IMG_PROJECT;
import remato.client.model.facade.INodeParent;
import remato.client.model.facade.IPropertiesContainer;
import remato.client.view.projecttree.CreateCategoryAction;
import remato.client.view.projecttree.CreateProjectAction;
import remato.client.view.util.ViewerAction;
import remato.common.domain.helpers.Nameable;
import remato.common.domain.helpers.ProjectParentable;
import remato.domain.common.Project;
import remato.domain.common.RequirementSystem;

import com.wgo.precise.client.ui.view.action.GetPdfAction;

/**
 * 
 * @author Petter L. H. Eide
 * @version $Id: ProjectWrapper.java,v 1.5 2006-02-19 16:29:37 petterei Exp $
 */
public class ProjectWrapper extends NodeParent<INodeParent, NodeChild> {

	private static final Class PARENT_INTERFACE = ProjectParentable.class;
	private static final String GET_CHILDREN_METHODS_NAME[] = {"getProjectChildren", "getCategoryChildren"};
	
	/** The domain model object to wrap */
    private Project project;
    
    /**
     * For use when instantiated with new domain model object.
     * 
     * @param project The Domain Model object to wrap
     */
    protected ProjectWrapper(Project project) {
        super(project, PARENT_INTERFACE, null, GET_CHILDREN_METHODS_NAME);
      	 init(project);
    }
    
    /**
     * For use when instantiated with existing domain model object from server.
     * 
     * @param project The Domain Model object to wrap
     * @param wrapper The parent wrapper
     */
    protected ProjectWrapper(Project project, RootProjectSystemWrapper wrapper) {
        super(project,wrapper, PARENT_INTERFACE, null,GET_CHILDREN_METHODS_NAME);
    }
    
    /**
     * For use when instantiated with existing domain model object from server.
     * 
     * @param project The Domain Model object to wrap
     * @param wrapper The parent wrapper
     */
    protected ProjectWrapper(Project project, ProjectWrapper wrapper) {
        super(project,wrapper, PARENT_INTERFACE, null, GET_CHILDREN_METHODS_NAME);
    }
    
    /**
	 * @see remato.client.model.NodeChild#init(remato.common.domain.helpers.Nameable)
	 */
	@Override
	protected void init(Nameable nameable) {
		this.project = (Project)nameable;
	}

	@Override
    public List<ViewerAction> getMenuItemActions() {
        List<ViewerAction> actionList= new ArrayList<ViewerAction>();
        actionList.add(new CreateProjectAction(this));
        actionList.add(new CreateCategoryAction(this));
//        actionList.add(new CreatePdfAction(RequirementPlugin.getInstance().getActiveShell().getShell(),project));
        actionList.add(new GetPdfAction(RequirementPlugin.getInstance().getActiveShell().getShell(), this));
        actionList.addAll(super.getMenuItemActions());
        return actionList;
    }

    public int getSortingOrder() {
        return Configuration.PROJECT_SORT_ORDER;
    }

	/**
	 * For DND
	 * 
	 * @see remato.client.model.facade.DNDTarget#validTransfer(remato.client.model.ClientModelItem)
	 */
    @Override
	public boolean validTransfer(IPropertiesContainer type) {
		boolean valid = false;
		if (type instanceof ProjectWrapper || type instanceof CategoryWrapper) {
			valid = true;
		}
		return valid && super.validTransfer(type);
	}
    
	@Override
	public Image getImage() {
		return IMG_PROJECT.getImage();
	}

	/* (non-Javadoc)
	 * @see remato.client.model.NodeChild#setParent(T)
	 */
	@Override
	public void setParent(INodeParent newParent) {
		if ( ( getParent() instanceof ClientSystemWrapper && newParent != getParent() ) || null == getParent()) { // hack because root project does not have parent in domain-model..
			// it would probalby be better to restore hierarchy in domain model..
			RequirementSystem system = (RequirementSystem)RequirementPlugin.getInstance().getSession().getWrapperFactory().<ProjectWrapper>getSystem(ProjectWrapper.class).getDomainModelObject(); 
			if (system.getProjectChildren().contains(getDomainModelObject())) {
				system.removeProjectChild((Project)getDomainModelObject());
			}
		}
		super.setParent(newParent);
	}
	
}
